// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

// 用于玩家自己的头像脚本 与 PlayerBoard类似

var Take6Model = require("Take6Model");

cc.Class({
    extends: cc.Component,

    properties: {

        playerProgress: {
            default: null,
            type: cc.Sprite
        },

        m_flAddProgressPerSecond : 0.2,
        m_bRunProgress : false,
        m_nTimeLeft : 5,
        m_bStartCountDown : false,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    progressCallBack : function ()
    {
        if (this.m_bRunProgress)
        {
            this.addProgress(this.playerProgress);
        }
        else
        {
            this.unschedule(this.progressCallBack);
        }
    },

    startProgress : function (countDownFuction, rgPlayerCards)
    {
        this.m_countDownFuction = countDownFuction;
        this.m_rgPlayerCards = rgPlayerCards;

        this.m_bRunProgress = true;
        this.playerProgress.fillRange = 0;

        var statusLabel = this.node.getChildByName('statusLabel');
        statusLabel.active = false;

        this.schedule(this.progressCallBack, this.m_flAddProgressPerSecond);
    },

    addProgress : function (sprite)
    {
        var nTimeSum = Take6Model.m_nWaitSendCardTime;

        var fillRange = sprite.fillRange;
        if (fillRange < 1)
        {
            fillRange += this.m_flAddProgressPerSecond / nTimeSum;
        }

        if (fillRange >= 1)
        {
            fillRange = 1;
            sprite.fillRange = fillRange;
            
            this.m_bRunProgress = false;
        }
        else
        {
            sprite.fillRange = fillRange;

            var timeLeft = (1 - fillRange) * nTimeSum;
            if (timeLeft <= 5 && this.m_bStartCountDown == false)
            {
                this.m_bStartCountDown = true;
                this.startCountDownProgress();
            }
        }
    },

    clearProgress : function ()
    {
        this.m_bRunProgress = false;
        this.playerProgress.fillRange = 0;

        var statusLabel = this.node.getChildByName('statusLabel');
        statusLabel.active = false;
    },

    endProgress : function ()
    {
        this.m_bRunProgress = false;
        this.playerProgress.fillRange = 1;
        this.m_bStartCountDown = false;

        var statusLabel = this.node.getChildByName('statusLabel');
        statusLabel.getComponent(cc.Label).string = "ready";
        statusLabel.active = true;

        this.unschedule(this.countDownCallBack);
    },

    startCountDownProgress : function ()
    {
        this.schedule(this.countDownCallBack, 1);
    },

    countDownCallBack : function ()
    {
        if (this.m_nTimeLeft > 0)
        {
            var node = new cc.Node("label");
            node.scale = 5;
            node.oppacity = 100;
            node.localZorder = 10;

            var action1 = cc.fadeIn(0.3);
            var action2 = cc.delayTime(0.2);
            var action3 = cc.fadeOut(0.3);
            var action4 = cc.scaleTo(0.3, 1.3);

            var actionUnion1 = cc.spawn(action4, cc.sequence(action1, action2, action3));

            var label = node.addComponent(cc.Label);
            label.string = this.m_nTimeLeft.toString()

            node.runAction(actionUnion1);

            node.parent = this.node;
            this.m_nTimeLeft--;
        }
        else
        {
            this.m_nTimeLeft = 5;
            this.unschedule(this.countDownCallBack);
            this.m_countDownFuction(this.m_rgPlayerCards);
        }
    },

    // start () {

    // },

    // update (dt) {},
});
